package com.polaris.live.viewmodel

import com.polaris.live.common.cacha.UserManager
import com.polaris.live.common.constant.CommonConst
import com.polaris.live.common.mvvm.viewmodel.BaseViewModel
import com.polaris.live.common.network.request
import com.polaris.live.common.network.state.ResultState
import com.polaris.live.common.util.ToastGravity
import com.polaris.live.constant.UserConst
import com.polaris.live.net.liveService
import com.polaris.live.net.payerService
import com.polaris.live.net.userService
import com.polaris.live.resp.back_resp.InfoOtherResp
import com.polaris.live.resp.back_resp.LiveAdministratorResp
import com.polaris.live.resp.back_resp.LiveUserOnlineResp
import com.polaris.live.resp.back_resp.PayerRechargeDailyRewardsPriceResp
import com.polaris.live.resp.back_resp.PayerRechargePriceListVo
import com.polaris.live.resp.back_resp.PayerRechargeWeekPackPriceResp
import com.polaris.live.resp.back_resp.RechargePriceResp
import com.polaris.live.resp.back_resp.UserCustomerServiceResp
import com.polaris.live.resp.back_resp.UserTaskListResp
import com.polaris.live.utils.BitwiseUtils
import com.polaris.live.utils.OtherUserCacheUtils

/**
 * 全局公共的请求处理
 *
 * 这里不持有viewModel
 *
 * @author Created by 半仙 on 2023/10/13/013 20:28
 */
object GlobalViewModelProxy {

    /**
     * 获取个人信息
     */
    fun getInfoUser(
        viewModel: BaseViewModel,
        userId: Long,
        block: ((userInfo: InfoOtherResp) -> Unit),
    ) {
        val type = BitwiseUtils.bitwiseOperation(
            longArrayOf(
                UserManager.BASIC_INFORMATION,
                UserManager.RELATIONSHIP_INFORMATION,
                UserManager.ALBUM_INFORMATION,
                UserManager.CALL_INFORMATION,
                UserManager.LIVE_INFORMATION,
                UserManager.HONOR_INFORMATION,
                UserManager.BEAUTIFUL_NUMBER,
                UserManager.DECORATIVE_DISPLAY
            )
        )
        viewModel.request({ userService.getInfoOther(userId, type, UserConst.From.USER_CARD) }, {
            block(it)
        }, {
            when (it.errCode) {
                CommonConst.ErrorCode.USER_LOGGED_OUT, //用户已注销
                CommonConst.ErrorCode.ACCOUNT_SHUT_DOWN,
                -> {//该账号已经被关小黑屋
                    OtherUserCacheUtils.removeUser(userId)
                }
            }
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    /**
     * 充值列表
     */
    fun getRechargeList(
        viewModel: BaseViewModel,
        error: () -> Unit = {},
        block: (rechargeInfo: RechargePriceResp) -> Unit,
    ) {
        viewModel.request({ payerService.rechargePrice() }, {
            block(it)
        }, {
            error.invoke()
            ToastGravity.showGravity(it.errorMsg)
        })
    }

    /**
     * 今日特价
     */
    fun getTodayRecharge(
        viewModel: BaseViewModel,
        block: (ResultState<List<PayerRechargePriceListVo>>) -> Unit,
    ) {
        viewModel.request({ payerService.getTodayPromotion() }, {
            block(ResultState.onAppSuccess(it))
        }, {
            block(ResultState.onAppError(it))
        })
    }

    /**
     * 七日限时礼包查询
     */
    fun getWeekRecharge(
        viewModel: BaseViewModel,
        block: (ResultState<List<PayerRechargeWeekPackPriceResp>>) -> Unit,
    ) {
        viewModel.request({ payerService.getWeekPrice() }, {
            block(ResultState.onAppSuccess(it))
        }, {
            block(ResultState.onAppError(it))
        })
    }

    /**
     * 用户在线列表
     */
    fun getOnlineUser(
        viewModel: BaseViewModel,
        liveId: Long,
        block: (LiveUserOnlineResp) -> Unit,
    ) {
        viewModel.request({ liveService.getOnlineUser(liveId) }, {
            block.invoke(it)
        })
    }

    /**
     * 任务弹窗
     */
    fun getTaskData(
        viewModel: BaseViewModel, pageType: Int,
        block: (UserTaskListResp) -> Unit,
    ) {
        viewModel.request({ userService.getTaskList(pageType) }, {
            block.invoke(it)
        })
    }

    /**
     * 获取管理员列表
     */
    fun getAdministratorData(
        viewModel: BaseViewModel,
        block: (LiveAdministratorResp) -> Unit,
    ) {
        viewModel.request({ liveService.getAdministratorList() }, {
            block.invoke(it)
        })
    }

    /**
     * 充值活动-每日领取
     */
    fun getRechargeActivity(
        viewModel: BaseViewModel,
        block: (ResultState<List<PayerRechargeDailyRewardsPriceResp>>) -> Unit,
    ) {
        viewModel.request({ payerService.getRechargeAc() }, {
            block.invoke(ResultState.onAppSuccess(it))
        }, {
            block(ResultState.onAppError(it))
        })
    }

    /**
     * 客服入口
     */
    fun getCustomerService(
        viewModel: BaseViewModel,
        block: (ResultState<UserCustomerServiceResp>) -> Unit,
    ) {
        viewModel.request({ userService.customerService() }, {
            block.invoke(ResultState.onAppSuccess(it))
        })
    }
}